<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript变量与常量</title>
</head>
<body>
    <h1>JavaScript变量与常量</h1>
    <script>
        a = 1;
        console.log(a);
        // let 定义块级变量
        let b = "let定义变量";
        console.log(b);

        // var 全局定义
        var c = "var定义变量";
        console.log(c);

        // 变量：合法的utf-8字符
        let 学号 = "中文变量名";
        console.log(学号);

        // let 和 var 作用域不同 var提升变量作用域
        console.log(d);
        var d = "var提升变量作用域";

        //console.log(e);  //报错：引用错误
        let e = "let作用域在定义的范围之内";

        if(true){
            var f = "全局变量";
            // let f = "语句块作用域";
        }
        console.log(f);

        //var可以重复定义变量
        var g = "var重复定义";
        var g = "var重复定义";

        //let定义变量
        let h = "let重复定义失败";
        // let h = "let重复定义失败";

        //常量定义
        const PI = 3.14;
        //PI = 3;   //访问错误。  Uncaught TypeError: Assignment to constant variable.

        //声明为常量的数组和对象，其“内容”不被保护
        const arr = [1,2];
        console.log(arr);
        arr.push(3);
        console.log(arr);

        //动态数据类型
        let myNumber = 500;  //数值
        console.log(typeof myNumber);
        myNumber = "500";  //字符串类型
        console.log(typeof myNumber);
        myNumber = true;  //布尔类型
        console.log(typeof myNumber);
    </script>
</body>
</html>